package com.web.domain;

/**
 * Created by Administrator on 2017/3/6.
 */

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Builder;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 返回
 *
 * @author ekoo
 */
@Builder
@AllArgsConstructor
public class Response<T> implements Serializable {
    public static Integer SUCCESS = 900000;
    public static Integer FAIL = 999999;
    /**
     * 900000成功，非900000失败
     **/
    private int status;
    /**
     * 错误具体内容
     */
    private String message;
    protected T data;
    private long timestamp;
    protected T params;


    public Response() {
        this.timestamp = System.currentTimeMillis();
    }

    public static Response failure() {
        Response response = new Response();
        response.status = 999999;
        response.message = "fail";
        response.timestamp = System.currentTimeMillis();
        return response;
    }

    public static Response failure(Integer status, String message) {
        Response response = new Response();
        response.status = status;
        response.message = message;
        response.timestamp = System.currentTimeMillis();
        return response;
    }

    public static Response failure(String message) {
        Response response = new Response();
        response.status = 0;
        response.message = message;
        response.timestamp = System.currentTimeMillis();
        return response;
    }

    public static Response ok() {
        Response response = new Response();
        response.status = 900000;
        response.message = "success";
        response.timestamp = System.currentTimeMillis();
        return response;
    }

    public static Response ok(String message) {
        Response response = new Response();
        response.status = 900000;
        response.message = message;
        response.timestamp = System.currentTimeMillis();
        return response;
    }

    @JsonIgnore
    public Boolean isOk() {
        if (status == 900000)
            return true;
        else return false;
    }

    public Response ok(T data) {
        this.status = 900000;
        this.message = "success";
        this.timestamp = System.currentTimeMillis();
        this.data = data;
        return this;
    }

    public static Response custom() {
        Response response = new Response();
        return response;
    }

    public static Response List() {
        Response<List> response = new Response<List>();
        if (response.getData() == null) {
            response.setData(Lists.newArrayList());
        }
        return response;
    }

    public static Response Map() {
        Response<Map> response = new Response<Map>();
        if (response.getData() == null) {
            response.setData(Maps.newHashMap());
        }
        return response;
    }

    public static Response Map(String key, Object value) {
        Response<Map> response = new Response<Map>();
        if (response.getData() == null) {
            response.setData(Maps.newHashMap());
            response.getData().put(key, value);
        }
        return response;
    }

    public static Response Map(Map map) {
        Response<Map> response = new Response<Map>();
        if (response.getData() == null) {
            response.setData(Maps.newHashMap());
            response.getData().putAll(map);
        }
        return response;
    }

    public Response addParams(T data) {
        this.status = 900000;
        this.message = "success";
        this.timestamp = System.currentTimeMillis();
        this.data = data;
        return this;
    }

    public Response put(String key, Object value) {
        ((Map) this.getData()).put(key, value);
        return this;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return StringUtils.defaultString(message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

}